package com.mtons.member.account.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.mtons.core.entity.po.AccountPO;
import com.mtons.core.interfaces.dao.AbstractDao;
import com.mtons.core.interfaces.dao.IAccountDao;

@Repository("accountDao")
public class AccountDao extends AbstractDao<Integer, AccountPO> implements IAccountDao {

	@Override
	public AccountPO findById(int id) {
		return getByKey(id);
	}

	@Override
	public void saveAccount(AccountPO po) {
		persist(po);
	}

	@Override
	public void deleteAccount(String ssn) {
		Query query = getSession().createQuery("delete from account where ssn = :ssn");
		query.setString("ssn", ssn);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountPO> findAllAccount() {
		Criteria criteria = createEntityCriteria();
		return criteria.list();
	}

	@Override
	public AccountPO findAccountBySsn(String ssn) {
		Criteria criteria = createEntityCriteria();
		criteria.add(Restrictions.eq("ssn", ssn));
		return (AccountPO) criteria.uniqueResult();
	}

}
